package com.fenda.jetpacknavigation.roomdata;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelStoreOwner;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.widget.Button;

import com.fenda.jetpacknavigation.R;

import java.util.concurrent.TimeUnit;

import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;

public class RoomActivity extends AppCompatActivity implements MyAdapter.onCheck {

    private static final String TAG = "RoomActivity";
    private MyUserViewModel viewModel;
    private MyAdapter myAdapter;
    private RecyclerView recyclerView;
    private Button insert, delete;
    private final CompositeDisposable mDisposable = new CompositeDisposable();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_room);

        recyclerView = findViewById(R.id.recycle);
        insert = findViewById(R.id.btninsert);
        delete = findViewById(R.id.btndelete);

        viewModel = new ViewModelProvider(this, new ViewModelProvider.AndroidViewModelFactory(this.getApplication())).get(MyUserViewModel.class);
        myAdapter = new MyAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(myAdapter);


        insert.setOnClickListener(v -> {
            insert.setClickable(false);
            String[] firstName = {
                    "你好",
                    "大佬",
                    "美丽",
                    "世界"
            };
            String[] lastName = {
                    "sdk",
                    "ndk",
                    "ios",
                    "android"
            };
            int name = (int) (Math.random() * 4);
            mDisposable.add(viewModel.insert(new User(firstName[name], lastName[name]))
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(() -> {
                                        insert.setClickable(true);
//                                myAdapter.notifyDataSetChanged();
                                    }, throwable -> Log.e(TAG, "Unable to get username", throwable)
                            )
            );
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        mDisposable.add(viewModel.getUsers()
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
//                .debounce(500, TimeUnit.SECONDS)
                        .subscribe(lists -> {
                            if (myAdapter.getItemCount() != lists.size()) {
                                myAdapter.setData(lists, this);
                                myAdapter.notifyDataSetChanged();
                            }
                        }, throwable -> Log.e(TAG, "Unable to get username", throwable))
        );
    }

    @Override
    public void setFlag(User user) {
        mDisposable.add(viewModel.updateUser(user)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        () -> {
                            myAdapter.notifyItemChanged(user.getPosition());
                        }
                        , throwable -> Log.e(TAG, "Unable to get username", throwable)
                )
        );
    }

    @Override
    protected void onStop() {
        super.onStop();
        mDisposable.clear();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mDisposable.dispose();
    }
}